import multiprocessing

import requests

from web_server import HttpServerEasy


def print_info(info, r):
    print(info, "\nr.text = ", r.text)
    print("\n")


if __name__ == '__main__':
    http_server = HttpServerEasy(8080, "static", print_log=False)
    start_server = multiprocessing.Process(target=http_server.start_server)
    start_server.start()

    r = requests.get("http://localhost:8080")
    print_info('测试访问根目录', r)

    data = {"username": "sundy"}
    r = requests.post("http://localhost:8080/login", data=data)
    print_info('测试登陆', r)

    r = requests.post("http://localhost:8080/login", data=data,
                      cookies=r.cookies)
    print_info("测试有cookies的登陆请求", r)

    data = {"username": "sundy11"}
    r = requests.post("http://localhost:8080/login", data=data)
    print_info('测试错误的用户名登陆', r)

    r = requests.get("http://localhost:8080/say_list")
    print_info('测试访问所有留言列表', r)

    data = {'say_content': "hello world!"}
    r = requests.post("http://localhost:8080/say_list", data=data)
    print_info('测试添加留言（未登录）', r)

    data = {"username": "sundy", 'say_content': "hello world!"}
    r = requests.post("http://localhost:8080/login", data=data)
    r = requests.post("http://localhost:8080/say_list", cookies=r.cookies,
                      data=data)
    print_info('测试添加留言（登陆）', r)

    r = requests.get("http://localhost:8080/say_list")
    print_info('测试访问所有留言列表', r)
